package com.crm.config;

import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.model.mpojo.Users;
import com.crm.util.LoginUtil;


public class LoginFilter implements Filter {

	@Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    	HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        System.out.println(req.getRequestURI());
        Users sysUser = LoginUtil.getUser();
        if (sysUser == null) {
            String path = "/login/log";
            resp.sendRedirect(path);
            return;
        }
        req.setAttribute("user", sysUser);
        RequestHolder.add(sysUser);
        RequestHolder.add(req);
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }
    @Override
    public void destroy() {

    }
	

	
}